﻿using _15Net8AspNetCore_ORM.Models;
using _15Net8AspNetCore_ORM.OrderRespository;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace _15Net8AspNetCore_ORM.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class RespositoryOrderController : ControllerBase
    {
        public IOrderRespository _orderRespository;
        public RespositoryOrderController(IOrderRespository orderRespository) 
        {
            _orderRespository = orderRespository;
        }

        /// <summary>
        /// 新增商品
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> AddOrder(Order Order)
        {
            Order = _orderRespository.AddOrder(Order);
            return CreatedAtAction(nameof(GetOrder), new { id = Order.Id }, Order);
        }


        /// <summary>
        /// 删除商品
        /// </summary>
        [HttpDelete("id")]
        public async Task<IActionResult> DeleteOrder(int id)
        {
            _orderRespository.DeleteOrder(id);
            return Ok("删除成功");
        }

        /// <summary>
        /// 修改商品
        /// </summary>
        [HttpPut("id")]
        public async Task<IActionResult> UpdateOrder(int id, Order Order)
        {

            //1、设置更新状态
            _orderRespository.UpdateOrder(id, Order);
            return Ok("更新成功");
        }

        /// <summary>
        /// 获取商品
        /// </summary>
        [HttpGet("id")]
        public async Task<IActionResult> GetOrder(int id)
        {
            Order order = _orderRespository.GetOrder(id);
            return Ok(order);
        }

        /// <summary>
        /// 获取商品
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> GetAllOrder()
        {
            List<Order> orders = _orderRespository.GetAllOrder();
            return Ok(orders);
        }
    }
}
